package com.yinhai.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.widget.Toast;

import com.cmcc.groupcontacts.MainApp;
import com.yinhai.cfg.AppConfig;
import com.yinhai.model.UserInfo;

public class Util {
    
    public static final void showMsg(Context context, String msg) {
        if (context == null || NullUtils.isEmpty(msg)) {
            return;
        }
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
    
    public static String object2String(Object object) {
        
        // 创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 创建对象输出流，并封装字节流
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            // 将对象写入字节流
            oos.writeObject(object);
            // 将字节流编码成base64的字符窜
            return new String(Base64.encode(baos.toByteArray()));
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    public static Object string2Object(String str) {
        byte[] base64 = Base64.decode(str);
        
        // 封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            // 再次封装
            ObjectInputStream bis = new ObjectInputStream(bais);
            try {
                // 读取对象
                return bis.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static UserInfo getUserInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
        UserInfo userInfo = new UserInfo();
        userInfo.setName(preferences.getString("name", ""));
        userInfo.setPassword(preferences.getString("password", ""));
        userInfo.setUser_code(preferences.getString("user_code", ""));
        userInfo.setFirstStart(preferences.getBoolean("isFirstStart", true));
        userInfo.setLoginSuccess(preferences.getBoolean("isLoginSuccess", false));
        userInfo.setIm_user_name(preferences.getString("im_user_name", ""));
        userInfo.setIm_user_password(preferences.getString("im_user_password", ""));
        userInfo.setVersion(preferences.getInt("version", 0));
        userInfo.setVersion1(preferences.getInt("version1", 0));
        return userInfo;
    }
    
    public static boolean isConferenceTop(Context context) {
        boolean result = false;
        ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(20);
        String packageName = context.getPackageName();
        if (runningTaskInfos != null && runningTaskInfos.size() > 0) {
            // 应用程序位于堆栈的顶层
            for (RunningTaskInfo runningTaskInfo : runningTaskInfos) {
                if (runningTaskInfo == null || runningTaskInfo.topActivity == null)
                    continue;
                if (packageName.equals(runningTaskInfo.topActivity.getPackageName())) {
                    String top_activityName = (runningTaskInfo.topActivity).getClassName();
                    if (!TextUtils.isEmpty(top_activityName))
                        return true;
                }
            }
            
        }
        return result;
    }
    
    public static boolean isConnect(Context context) {
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        try {
            ConnectivityManager connectivity =
                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                // 获取网络连接管理的对象
                NetworkInfo info = connectivity.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    // 判断当前网络是否已经连接
                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
        }
        return false;
    }
}
